<?php


namespace app\admin\controller\System;


use app\admin\controller\BaseController;
use app\admin\model\System\SysSettingModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\Request;

class SysSetting extends BaseController
{

    /**
     * 获取系统参数
     * @param Request $request
     * @return array
     */
    public function getSysSetting(Request $request)
    {
        $setting = SysSettingModel::getSysSettingWhere([['id', '=', 1]]);
        if (!empty($setting['sys_sales_setting'])) {
            $sales_setting = json_decode($setting['sys_sales_setting'], true);
            $setting['level'] = $sales_setting['level'];
            $setting['level_sales0'] = $sales_setting['level_sales0'];
            $setting['level_sales1'] = $sales_setting['level_sales1'];
            $setting['level_sales2'] = $sales_setting['level_sales2'];
        } else {
            $setting['level'] = 0;
            $setting['level_sales0'] = 0;
            $setting['level_sales1'] = 0;
            $setting['level_sales2'] = 0;
        }
        if (!empty($setting['customer_info'])) {
            $customer_setting = json_decode($setting['customer_info'], true);
            $setting['wx_info_number'] = $customer_setting['wx_info_number'];
            $setting['wx_info_url'] = $customer_setting['wx_info_url'];
            $setting['qq_info_number'] = $customer_setting['qq_info_number'];
            $setting['qq_info_url'] = $customer_setting['qq_info_url'];
        } else {
            $setting['wx_info_number'] = "";
            $setting['wx_info_url'] = "";
            $setting['qq_info_number'] = "";
            $setting['qq_info_url'] = "";
        }
        if (!empty($setting['study_data'])) {
            $study_data = json_decode($setting['study_data'], true);
            $setting['total_price'] = $study_data['total_price'];
            $setting['deduction_price'] = $study_data['deduction_price'];
            $setting['is_diploid'] = $study_data['is_diploid'];
        } else {
            $setting['total_price'] = "";
            $setting['deduction_price'] = "";
            $setting['is_diploid'] = 0;
        }

        if (!empty($setting['aliyun'])) {
            $setting['aliyun'] = json_decode($setting['aliyun'], true);
        } else {
            $setting['aliyun']['app_id'] = "";
            $setting['aliyun']['secret'] = "";
            $setting['aliyun']['sign'] = "";
            $setting['aliyun']['template'] = "";
        }

        if (!empty($setting['sys_extend'])) {
            $sys_extend = json_decode($setting['sys_extend'], true);
            $setting['contact_way'] = $sys_extend['contact_way'];
            $setting['about_us'] = htmlspecialchars_decode($sys_extend['about_us']);
        } else {
            $setting['contact_way'] = "";
            $setting['about_us'] = "";
        }

        $setting['group_info'] = htmlspecialchars_decode($setting['group_info']);
        $setting['default_head'] = ImageUtil::i()->replaceUrl($setting['default_head']);

        return Util::returnJson(self::CODE_DATA, $setting, "");
    }

    /**
     * 修改
     * @param Request $request
     * @return array
     */
    public function editSysSetting(Request $request)
    {
        $param = $request->param();
        $sales_setting = [
            'level' => $param['level'],
            'level_sales0' => $param['level_sales0'],
            'level_sales1' => $param['level_sales1'],
            'level_sales2' => $param['level_sales2'],
        ];

        $customer_setting = [
            'wx_info_number' => $param['wx_info_number'],
            'wx_info_url' => !empty($param['wx_info_url']) ? $param['wx_info_url'] : "",
            'qq_info_number' => $param['qq_info_number'],
            'qq_info_url' => !empty($param['qq_info_url']) ? $param['qq_info_url'] : ""
        ];

        $study_data = [
            'total_price' => $param['total_price'],
            'deduction_price' => $param['deduction_price'],
            'is_diploid' => !empty($param['is_diploid']) ? 1 : 0
        ];

        $aliyun = [
            'app_id' => $param['app_id'],
            'secret' => $param['secret'],
            'sign' => $param['sign'],
            'template' => $param['template'],
        ];

        $sys_extend = [
            'contact_way' => $param['contact_way'],
            'about_us' => htmlspecialchars($param['about_us'])
        ];

        if (empty($param['id'])) {
            $data = [
                'img_url' => $param['img_url'],
                'api_url' => $param['api_url'],
                'sys_pay_time' => $param['sys_pay_time'],
                'sys_is_sales' => $param['sys_is_sales'],
                'sys_sales_setting' => json_encode($sales_setting),
                'only_way' => $param['only_way'],
                'customer_info' => json_encode($customer_setting),
                'study_data' => json_encode($study_data),
                'aliyun' => json_encode($aliyun),
                'sys_extend' => json_encode($sys_extend),
                'app_version' => !empty($param['app_version']) ? $param['app_version'] : 1,
                'version_name' => !empty($param['version_name']) ? $param['version_name'] : "",
                'group_info' => !empty($param['group_info']) ? htmlspecialchars($param['group_info']) : "",
                'teacher_text' => !empty($param['teacher_text']) ? htmlspecialchars($param['teacher_text']) : "",
                'wx_number' => !empty($param['wx_number']) ? htmlspecialchars($param['wx_number']) : "",
                'phone' => !empty($param['phone']) ? htmlspecialchars($param['phone']) : "",

            ];
            if (!empty($param['app_url'])) {
                $data['app_url'] = $param['app_url'];
            }
            //图片上传(单图)
            if (!empty($request->file('file'))) {
                $file = $request->file('file');
                $path = "/user/default/img/";
                $url = ImageUtil::i()->upload($file, $path);
                $data['default_head'] = $url;
            }

            $id = SysSettingModel::addSysSetting($data);
            if ($id) {
                return Util::returnJson(self::CODE_SUCCESS, null, "保存成功");
            } else {
                return Util::returnJson(self::CODE_ERROR, null, "保存失败1");
            }
        } else {
            $save = [
                'img_url' => $param['img_url'],
                'api_url' => $param['api_url'],
                'sys_pay_time' => $param['sys_pay_time'],
                'sys_is_sales' => $param['sys_is_sales'],
                'sys_sales_setting' => json_encode($sales_setting),
                'only_way' => $param['only_way'],
                'customer_info' => json_encode($customer_setting),
                'study_data' => json_encode($study_data),
                'aliyun' => json_encode($aliyun),
                'sys_extend' => json_encode($sys_extend),
                'app_version' => !empty($param['app_version']) ? $param['app_version'] : 1,
                'version_name' => !empty($param['version_name']) ? $param['version_name'] : "",
                'group_info' => !empty($param['group_info']) ? htmlspecialchars($param['group_info']) : ""
            ];

            if (!empty($param['app_url'])) {
                $data['app_url'] = $param['app_url'];
            }

            //图片上传(单图)
            if (!empty($request->file('file'))) {
                $file = $request->file('file');
                $path = "/user/default/img/";
                $url = ImageUtil::i()->upload($file, $path);
                $save['default_head'] = $url;
            }

            $flag = SysSettingModel::updateSysSettingWhere([['id', '=', $param['id']]], $save);
            if ($flag) {
                return Util::returnJson(self::CODE_SUCCESS, null, "保存成功");
            } else {
                return Util::returnJson(self::CODE_ERROR, null, "保存失败2");
            }
        }

    }

}
